import React, { useState, useEffect, useMemo } from 'react';
import { Title } from 'base/ctl/title';
import { loc } from 'local';
import { g_wsn } from 'db/state';
import { Time } from 'base/ctl/base';
import { cline, uDb, GateWay, uXls, obj_str, loc_date, is_empty, Wait, MaTable, d_sort, NuiBt } from 'sui';
import { adjust_val } from './cmd';

const PsnView = ({ hreturn, cur, title, mod, his=false }) => {
    const [main, setMain] = useState();
    const [wait, setWait] = useState(false);

    useEffect(() => {
        const sn = obj_str(cur, "sn");
        if (!sn) {
            return;
        }

        uDb(null, "psn/list",
            {
                line: cline(),
                k: sn
            },
            r => {
                adjust_val(r);

                if (mod === "pair") {
                    d_sort(r, "groupsn");
                }

                setMain(r);
            }, e => setMain(null));
    }, [cur, mod, his]);

    const getCol = useMemo(() => {
        const col = [
            {title: "型号", field: 'tech', width: 100},
            {title: "轴承编号", field: 'sn', width: 100},
            {title: "类别", field: 'gtp', width: 70},
            {title: "组配编号", field: 'groupsn', width: 100},
            {title: "外圆尺寸", field: 'outdia', width: 100},
            {title: "内孔尺寸", field: 'india', width: 100},
            {title: "宽度尺寸", field: 'outwidth', width: 100},
            {title: "b", field: 'standout', width: 100},
        ];

        if (mod === "any") {
            return col.filter(d => {
                const k = d.key;
                return k !== "groupsn" && k !== "gtp";
            });
        }

        return col;
    }, [mod]);

    const hout = () => {
        if (is_empty(main)) {
            alert("数据为空");
        }

        setWait(true);

        let h = getCol;
        const col = [];

        h = h.map(d => {
            col.push(d.field);

            return d.title;
        });

        const row = [h];
        const str_h = ["tech", "sn", "gtp", "groupsn"];
        const set = new Set();

        str_h.forEach(d => set.add(d));

        main.forEach(d => {
            const tmp = [];

            col.forEach(t => {
                let val = obj_str(d, t);
                if (set.has(t)) {
                    tmp.push(val);
                    return;
                }

                const f = parseFloat(val);
                if (f > 0) {
                    val = `+${val}`;
                }

                tmp.push(val);
            });

            row.push(tmp);
        });

        const tech = obj_str(cur, "tech");

        const path = `tmp/${tech}_${title}_${loc_date()}`;

        uXls(null, "write",
            {
                p: path,
                v: row,
            },
            r => {
                setWait(false);

                const out = `${GateWay.url_file()}/${path}.xls`;

                var link = document.createElement("a");
                link.download = "tech.zip";
                link.href = out;
                link.click();
            },
            e => {
                setWait(false);
                alert("导出数据错误");
            });
    };

    return (
        <div className="flex flex-col ml-2 mr-2">
            <div className="inline-flex items-center justify-between mt-2 mb-2">
                <div className="inline-flex">
                    <Title label={loc("base_43")} value={g_wsn()} width="w-56"/>
                    <Title label={title} width="w-56"/>
                </div>
                <Time/>
                <div className="inline-flex">
                    <NuiBt color="green" className="h-16 mr-4" hcmd={hout}>导出</NuiBt>
                    <NuiBt className="h-16 mr-2" hcmd={hreturn}>返回</NuiBt>
                </div>
            </div>
            <MaTable col={getCol} row={main} title={loc("start_38")} page={8} paging={true}
            opt={{
                minBodyHeight: 730,
                maxBodyHeight: 730,
            }}/>
            <Wait wait={wait}/>
        </div>
    );
};

export {PsnView};
